/*
 * Copyright (C) 2008 Rob Cash
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.robcash.commons.task;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import javax.swing.event.EventListenerList;
import org.robcash.commons.event.TaskEvent;
import org.robcash.commons.event.TaskListener;

/**
 *
 * @author Rob
 */
public class ThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor {

    /**
     * Event listeners
     */
    protected EventListenerList listeners = new EventListenerList();

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        fireTaskListenerTaskCompleted(new TaskEvent(this));
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        fireTaskListenerTaskStarted(new TaskEvent(this));
        super.beforeExecute(t, r);
    }

    /**
     * Add a new listener
     * @param listener Task listener
     */
    public void addTaskListener(TaskListener listener) {
        listeners.add(TaskListener.class, listener);
    }

    /**
     * Remove a new listener
     * @param listener Task listener
     */
    public void removeTaskListener(TaskListener listener) {
        listeners.remove(TaskListener.class, listener);
    }

    /**
     * Notify listeners
     * @param evt Task event to send to listeners
     */
    protected void fireTaskListenerTaskStarted(TaskEvent evt) {
        TaskListener[] taskListeners = listeners.getListeners(TaskListener.class);
        for( int index = 0; index < taskListeners.length; index++ ) {
            taskListeners[index].taskStarted(evt);
        }
    }

    /**
     * Notify listeners
     * @param evt Task event to send to listeners
     */
    protected void fireTaskListenerTaskCompleted(TaskEvent evt) {
        TaskListener[] taskListeners = listeners.getListeners(TaskListener.class);
        for( int index = 0; index < taskListeners.length; index++ ) {
            taskListeners[index].taskCompleted(evt);
        }
    }

}
